//
//  CYLTabBarControllerConfig.m
//  OAProject
//
//  Created by yhj on 2017/3/20.
//  Copyright © 2017年 cdnunion. All rights reserved.
//

#import "CYLTabBarControllerConfig.h"

#import "DynamicMainViewController.h"
#import "GoalMainViewController.h"
#import "WorkMainViewController.h"
#import "ContactMainViewController.h"
#import "MyMainViewController.h"


@interface BaseNavigationController : UINavigationController

@end

@implementation BaseNavigationController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationController.interactivePopGestureRecognizer.enabled=NO;
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.viewControllers.count > 0) {
        // 如果现在push的不是栈底控制器(最先push进来的那个控制器)
        // viewController.hidesBottomBarWhenPushed = YES;
        UIBarButtonItem *navigationSpace =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        navigationSpace.width = -5;

        //设置导航栏返回按钮
        UIBarButtonItem *backButton=[UIBarButtonItem itemWithImageName:@"back" highImageName:@"back" target:self action:@selector(back)];
        viewController.navigationItem.leftBarButtonItems=@[navigationSpace,backButton];
    }
    [super pushViewController:viewController animated:animated];
}

- (void)back
{
    [self popViewControllerAnimated:NO];
}

@end


@interface CYLTabBarControllerConfig()

@property(nonatomic,strong,readwrite)CYLTabBarController *tabBarController;

@end

@implementation CYLTabBarControllerConfig

-(CYLTabBarController *)tabBarController
{
    if (!_tabBarController) {
        CYLTabBarController *tabBarController=[CYLTabBarController tabBarControllerWithViewControllers:self.createTabBarViewControllers  tabBarItemsAttributes:self.createTabBarItemsAttributes];
        [self customizeTabBarAppearance:tabBarController];
        _tabBarController=tabBarController;
    }
    return _tabBarController;
}

//创建标签栏子控制器数组
-(NSArray *)createTabBarViewControllers
{

    // 消息
    DynamicMainViewController *messageVC=[DynamicMainViewController new];
    BaseNavigationController *messageNaV=[[BaseNavigationController alloc]initWithRootViewController:messageVC];

    // 目标
    GoalMainViewController *goalVC=[GoalMainViewController new];
    BaseNavigationController *goalNaV=[[BaseNavigationController alloc]initWithRootViewController:goalVC];

    // 工作
    WorkMainViewController *workVC=[WorkMainViewController new];
    BaseNavigationController *workNaV=[[BaseNavigationController alloc]initWithRootViewController:workVC];

    // 联系人
    ContactMainViewController *contactVC=[ContactMainViewController new];
    BaseNavigationController *contactNaV=[[BaseNavigationController alloc]initWithRootViewController:contactVC];

    // 我的
    MyMainViewController *myVC=[MyMainViewController new];
    BaseNavigationController *myNaV=[[BaseNavigationController alloc]initWithRootViewController:myVC];

    return @[messageNaV,goalNaV,workNaV,contactNaV,myNaV];
}

//创建标签栏按钮item数组
-(NSArray *)createTabBarItemsAttributes
{
    NSDictionary *messageDic=@{
                            CYLTabBarItemTitle:@"消息",
                            CYLTabBarItemImage:@"tab_message",
                            CYLTabBarItemSelectedImage:@"tab_message_select"
                            };

    NSDictionary *goalDic=@{
                            CYLTabBarItemTitle:@"目标",
                            CYLTabBarItemImage:@"tab_goal",
                            CYLTabBarItemSelectedImage:@"tab_goal_select"
                            };

    NSDictionary *workDic=@{
                            CYLTabBarItemTitle:@"工作",
                            CYLTabBarItemImage:@"tab_work",
                            CYLTabBarItemSelectedImage:@"tab_work_select"
                            };

    NSDictionary *contactDic=@{
                             CYLTabBarItemTitle:@"联系人",
                             CYLTabBarItemImage:@"tab_contact",
                             CYLTabBarItemSelectedImage:@"tab_contact_select"
                             };

    NSDictionary *myDic=@{
                            CYLTabBarItemTitle:@"我的",
                            CYLTabBarItemImage:@"tab_my",
                            CYLTabBarItemSelectedImage:@"tab_my_select"
                            };

    return @[messageDic,goalDic,workDic,contactDic,myDic];
}

// tabBarItem 的选中和不选中文字属性、背景图片
- (void)customizeTabBarAppearance:(CYLTabBarController *)tabBarController
{
    // 普通状态下的文字属性
    NSMutableDictionary *normalAttrs=[NSMutableDictionary dictionary];
    normalAttrs[NSForegroundColorAttributeName]=Color808080;

    // 选中状态下的文字属性
    NSMutableDictionary *selectedAttrs=[NSMutableDictionary dictionary];
    selectedAttrs[NSForegroundColorAttributeName]=MainColor;

    // 设置文字属性
    UITabBarItem *tabBar=[UITabBarItem appearance];
    [tabBar setTitleTextAttributes:@{
                                     NSForegroundColorAttributeName:Color808080,
                                     NSFontAttributeName:Font_Number(11),
                                     }forState:0];
    [tabBar setTitleTextAttributes:@{
                                     NSForegroundColorAttributeName:WhiteColor,
                                     NSFontAttributeName:Font_Number(11),
                                     }forState:UIControlStateSelected];

    [self customizeTabBarSelectionIndicatorImage];
}


- (void)customizeTabBarSelectionIndicatorImage
{
    ///Get initialized TabBar Height if exists, otherwise get Default TabBar Height.
    UITabBarController *tabBarController = [self cyl_tabBarController] ?: [[UITabBarController alloc] init];
    CGFloat tabBarHeight = tabBarController.tabBar.frame.size.height;
    CGSize selectionIndicatorImageSize = CGSizeMake(CYLTabBarItemWidth, tabBarHeight);
    //Get initialized TabBar if exists.
    UITabBar *tabBar = [self cyl_tabBarController].tabBar ?: [UITabBar appearance];
    [tabBar setSelectionIndicatorImage:
     [[self class] imageWithColor:MainColor
                             size:selectionIndicatorImageSize]];
}

+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
{
    if (!color || size.width <= 0 || size.height <= 0) return nil;
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width + 1, size.height);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

@end
